package com.swgk.core.view.pop;

import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.swgk.core.R;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.SectionAdapterOnItemClick;
import com.swgk.core.view.adapter.CategoryLeftAdapter3;
import com.swgk.core.view.adapter.CategoryRightAdapter3;
import com.swgk.core.view.entity.GoodsPublishEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.Iterator;
import java.util.List;

/**
 * @author Administrator
 * by 2021.03.23 修改布局
 */
public class GoodsTypePopWindow extends BasePopWindow<String> {
    private RecyclerView leftRecyclerView;
    private XRecyclerView rightRecyclerView;
    private CategoryLeftAdapter3 leftAdapter;
    private CategoryRightAdapter3 rightAdapter;
    private List<MenuEntity<MenuEntity<MenuEntity>>> datas;
    private TextView tvCancel;
    private TextView tvOk;
    private String cid1 = "";
    private String cid2 = "";
    private String cid3 = "";
    private GoodsPublishEntity goodsPublishEntity;
    private int index1;
    private int index3;
    private int index2 = -1;
    private String isSt = "";

    public GoodsTypePopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_goods_type;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        leftRecyclerView = holder.getPop().findViewById(R.id.left_recycler);
        rightRecyclerView = holder.getPop().findViewById(R.id.right_recycler);
        tvCancel = holder.getPop().findViewById(R.id.reset_view);
        tvOk = holder.getPop().findViewById(R.id.ok_view);
//        bgView = holder.getPop().findViewById(R.id.cover_layout);
        if (goodsPublishEntity == null) {
            goodsPublishEntity = new GoodsPublishEntity();
        }
       /* bgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
            }
        });*/
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("".equals(cid3)) {
                    Toast.makeText(mContext, "必须选择三级分类", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equals(cid1)) {
                    cid1 = datas.get(0).getId();
                    goodsPublishEntity.setCid1(cid1);
                    goodsPublishEntity.setCid1Name(datas.get(0).getName());
                }
                if (datas.get(index1).getDatas().size() != 0) {
                    if (datas.get(index1).getDatas().size() > index2) {
                        cid2 = datas.get(index1).getDatas().get(index2).getId();
                        goodsPublishEntity.setCid2(cid2);
                        goodsPublishEntity.setCid2Name(datas.get(index1).getDatas().get(index2).getName());
                        if (onResultClick != null) {
                            onResultClick.result(goodsPublishEntity, isSt, null);
                        }
                    }
                    dissMiss();
                } else {
                    Toast.makeText(mContext, "数据为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dissMiss();
            }
        });

        if (leftAdapter == null) {
            leftAdapter = new CategoryLeftAdapter3(mContext, null);
        }
        leftRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 1));
        leftRecyclerView.setAdapter(leftAdapter);
       /* leftRecyclerView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                mContext.getResources().getDimension(R.dimen.dp1), mContext.getResources().getColor(R.color.color_EDEDED)));*/
        leftAdapter.setItemSelect(true);
        leftAdapter.setDefItem(0);
        leftAdapter.setOnItemClick(new AdapterOnItemClick<MenuEntity<MenuEntity<MenuEntity>>>() {
            @Override
            public void onItemClick(MenuEntity<MenuEntity<MenuEntity>> menuEntity3MenuEntity3, int position) {
                cid1 = menuEntity3MenuEntity3.getId();
                goodsPublishEntity.setCid1(cid1);
                goodsPublishEntity.setCid1Name(menuEntity3MenuEntity3.getName());
                isSt = menuEntity3MenuEntity3.getName();
                if (index1 != position) {
                    cid3 = "";
                    index3 = position;
                } else {
                    cid3 = goodsPublishEntity.getCid3();
                    index1 = position;
                }
                showRightAdapter(menuEntity3MenuEntity3.getDatas());
            }
        });
        if (rightAdapter == null) {
            rightAdapter = new CategoryRightAdapter3(mContext, null);
        }
        rightAdapter.setItemSelect(true);
        rightAdapter.setDefItem(-1);
        rightRecyclerView.setSectionedRecyclerViewAdapter(rightAdapter);
        rightRecyclerView.setLoadingMoreEnabled(false);
        rightRecyclerView.setPullRefreshEnabled(false);
        GridLayoutManager rightManager = new GridLayoutManager(mContext, 3);
        rightRecyclerView.setLayoutManager(rightManager);
        rightRecyclerView.setAdapter(rightAdapter);
        rightAdapter.setOnItemClickListener(new SectionAdapterOnItemClick<MenuEntity>() {
            @Override
            public void onItemClick(MenuEntity menuEntity3, int section, int position) {
                if (goodsPublishEntity != null) {
                    cid3 = menuEntity3.getId();
                    if (!cid3.equals(goodsPublishEntity.getCid3())) {
                        index1 = index3;
                    }
                    goodsPublishEntity.setCid3(cid3);
                    goodsPublishEntity.setCid3Name(menuEntity3.getName());
                    index2 = section;
                }
            }
        });


        /*tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rightAdapter.setState(false);
                cid3 = "";
            }
        });*/
    }

    public void setData(List<MenuEntity<MenuEntity<MenuEntity>>> datas) {
        this.datas = datas;
        leftAdapter.setDatas(datas);
        if (datas.size() < 0) {
            return;
        }
        showRightAdapter(datas.get(index1).getDatas());
    }

    public void showRightAdapter(List<MenuEntity<MenuEntity>> data) {
        rightAdapter.setState(false);
        rightAdapter.setData(data);
    }

    /****
     *校验商品二级目录，如果二级目录下的三级目录没有数据，那么删除该数据不予展示
     * @param data 2021.03.25
     * @return
     */
    @Deprecated
    private List<MenuEntity<MenuEntity>> verificationMenuEntity(List<MenuEntity<MenuEntity>> data) {
        if (data == null || data.size() == 0) {
            return data;
        }
        Iterator<MenuEntity<MenuEntity>> it = data.iterator();
        while (it.hasNext()) {
            MenuEntity<MenuEntity> item = it.next();
            if (item.getDatas() == null || item.getDatas().size() == 0) {
                it.remove();
            }
        }
        return data;
    }
}
